<?php
namespace Swostar\Rpc;

use Swoole\Server;

/**
 * 多端口监听
 * @package Swostar\Rpc
 */
class Rpc
{
    protected $host;

    protected $port;

    public function __construct(Server $server, $config)
    {
        $listen = $server->listen($config['host'], $config['port'], SWOOLE_SOCK_TCP);
        $listen->set($config['swoole']);

        $listen->on('connect', [$this, 'connect']);
        $listen->on('receive', [$this, 'receive']);
        $listen->on('close', [$this, 'close']);

        echo '监听的地址为：' . $config['host']. ':' . $config['port'] . PHP_EOL;
    }

    public function connect($server, $fd)
    {
        echo 'rpc-connect' . PHP_EOL;
    }

    public function receive($server, $fd, $from_id, $data)
    {
        $server->send($fd, 'rpc-receive'. $data);
        $server->close($fd);
    }

    public function close($server, $fd)
    {

    }


}
